ggplot2The grammar of graphics: consistent aesthetics, multidimensional conditioning, and step-by-step plot building.
library(ggplot2)
p <- ggplot(mpg, aes(x=hwy, y=cty))
p + geom_point()
p <- ggplot(mtcars, aes(x=wt, y=mpg))
p + geom_point()
p + geom_point(aes(colour = factor(cyl)))
p + geom_point(aes(shape = factor(cyl)))
qsecp + geom_point(aes(size = qsec))
p + geom_point() + geom_smooth(method="lm")
p + geom_point(aes(colour = cyl)) + scale_colour_gradient(low = "blue")
p + geom_point(aes(shape = factor(cyl))) + scale_shape(solid = FALSE)
ggplot(mtcars, aes(wt, mpg)) + geom_point(colour = "red", size = 3)
Varying alpha is useful for large datasets
d <- ggplot(diamonds, aes(carat, price))
d + geom_point(alpha = 1/10)
d + geom_point(alpha = 1/20)
d + geom_point(alpha = 1/100)
Visualize a data transformation
..name.. syntaxstat_bin(geom="bar") OR geom_bar(stat="bin")b=ggplot(mpg,aes(fl))+geom_bar( aes(fill = fl)); b
b+scale_fill_brewer( palette = "Blues")
b+scale_fill_grey( start = 0.2, end = 0.8, na.value = "red")
a <- ggplot(mpg, aes(hwy)) + geom_dotplot( aes(fill = ..x..)); a
## stat_bindot: binwidth defaulted to range/30. Use 'binwidth = x' to adjust this.
gradienta + scale_fill_gradient( low = "red", high = "yellow")
## stat_bindot: binwidth defaulted to range/30. Use 'binwidth = x' to adjust this.
gradient2a + scale_fill_gradient2(low = "red", high = "blue", mid = "white", midpoint = 25)
## stat_bindot: binwidth defaulted to range/30. Use 'binwidth = x' to adjust this.
gradientna + scale_fill_gradientn( colours = rainbow(10))
## stat_bindot: binwidth defaulted to range/30. Use 'binwidth = x' to adjust this.